package com.nirvana.ui;

import com.nirvana.MainPanel;
import com.nirvana.item.ResolvePropertyItem;

import javax.swing.*;

public class TooltipEditPanel {
    private JComponent defGenPanel;
    private JComponent entityGenPanel;
    private JComponent settingPanel;
    private JComponent tooltipEditPanel;
    private JTextArea tooltipTextArea;
    private JButton tooltipCancelButton;
    private JButton tooltipSaveButton;
    private ResolvePropertyItem item;

    private static TooltipEditPanel INSTANCE;
    private TooltipEditPanel() {}

    public static TooltipEditPanel getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TooltipEditPanel();
        }
        return INSTANCE;
    }

    public void init(MainPanel mainPanel) {
        this.defGenPanel = mainPanel.configGeneratePanel;
        this.entityGenPanel = mainPanel.ormGeneratePanel;
        this.settingPanel = mainPanel.settingScrollPanel;
        this.tooltipEditPanel = mainPanel.tooltipEditPanel;
        this.tooltipTextArea = mainPanel.tooltipTextArea;
        this.tooltipCancelButton = mainPanel.tooltipCancelButton;
        this.tooltipSaveButton = mainPanel.tooltipSaveButton;
        this.tooltipCancelButton.addActionListener(e -> {
            close();
        });
        this.tooltipSaveButton.addActionListener(e -> {
            item.setTooltip(this.tooltipTextArea.getText());
            item = null;
            close();
        });
    }

    private void close() {
        defGenPanel.setVisible(false);
        entityGenPanel.setVisible(false);
        settingPanel.setVisible(true);
        tooltipEditPanel.setVisible(false);
    }

    public void open(ResolvePropertyItem item) {
        this.item = item;
        defGenPanel.setVisible(false);
        entityGenPanel.setVisible(false);
        settingPanel.setVisible(false);
        tooltipEditPanel.setVisible(true);
        tooltipTextArea.setText(item.getToolTip());
    }
}
